#include "bmpdroppertool.h"
#include "bmpeditor.h"

const char * BMPDropperTool::NAME = "Dropper";
const char * BMPDropperTool::getName(){return NAME;}

bool BMPDropperTool::pressedTool(const int & a_mouseButton)
{
	SPoint p(m_tool.getPosition());
	switch(a_mouseButton)
	{
	case SDL_BUTTON_LEFT:
	case SDL_BUTTON_RIGHT:
		BMPEditor* bmpe = (BMPEditor*)getContext();
		if(bmpe->getBMP()->getArea().contains(p))
		{
			int uiIndex = (a_mouseButton==SDL_BUTTON_LEFT)
				?BMPPalletteManager::COLOR_MAIN
				:BMPPalletteManager::COLOR_OFF;
			int colorIndex = bmpe->getBMP()->getPixelAt(p);
			*(bmpe->getPallette()->getSelectedColorIndex(uiIndex)) = colorIndex;
			int newIndex = *(bmpe->getPallette()->getSelectedColorIndex(uiIndex));
			bmpe->getPallette()->refresh();
		}
		return true;
	}
	return false;
}
bool BMPDropperTool::releasedTool(const int & a_mouseButton){return false;}
bool BMPDropperTool::draggedTool(const int & a_mouseButton)
{
	return pressedTool(a_mouseButton);
}

void BMPDropperTool::draw(SDL_Surface * a_surface)
{
	BMPEditor* bmpe = (BMPEditor*)getContext();
	Uint32 color = bmpe->getPallette()->getSelectedColor(
		BMPPalletteManager::COLOR_MAIN,a_surface->format);
	VSDL_drawRect(a_surface, &m_toolOnScreen, color, 1);
}